<?php
declare(strict_types=1);

namespace app\admin\business;

use app\common\model\CommonBase;
use app\common\model\WebsiteConfig as WebsiteConfigModel;

class WebsiteConfig extends AdminBase
{
    private $_websiteConfigObj = null;

    public function __construct(string $websiteCode)
    {
        $this->_websiteCode      = $websiteCode;
        $this->_websiteConfigObj = new WebsiteConfigModel([], $websiteCode);
    }

    public function getAllWebsiteConfig(array $where = []): array
    {
        $rows = null;
        try {
            $rows = $this->_websiteConfigObj->getAllWebsiteConfig($where);
        } catch (\Exception $e) {
            logErr('读取站点所有配置项失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return $rows ? $rows->toArray() : [];
    }

    public function getWebsiteConfigList(int $page = 1, int $limit = 10, array $where = []): array
    {
        $rows  = null;
        $count = 0;
        try {
            $rows  = $this->_websiteConfigObj->getWebsiteConfigList($page, $limit, $where);
            $count = $this->_websiteConfigObj->getWebsiteConfigCount($where);
        } catch (\Exception $e) {
            logErr('读取站点配置项列表失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        foreach ($rows as $key => $row) {
            $rows[$key]['status_name']       = $row['status'] == self::_IS_ENABLE_STATUS ? '启用' : '禁用';
            $rows[$key]['field_type_name']   = CommonBase::FIELD_TYPE_NAME_MAP[$row['field_type']] ?? '';
            $rows[$key]['config_group_name'] = WebsiteConfigModel::CONFIG_GROUP_MAP[$row['config_group']] ?? '';
        }

        return ['rows' => $rows ? $rows->toArray() : [], 'count' => $count];
    }

    public function addWebsiteConfig(string $cdnDomain, array $data): array
    {
        try {
            return [self::_RESULT => $this->_websiteConfigObj->addWebsiteConfig($cdnDomain, $data), self::_MESSAGE => self::_ADD_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('新增站点配置项失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function findWebsiteConfigById(int $id): array
    {
        $websiteConfig = null;
        try {
            $websiteConfig = $this->_websiteConfigObj->findWebsiteConfigById($id);
        } catch (\Exception $e) {
            logErr('通过主键读取站点配置项详细信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        if (empty($websiteConfig)) {
            return [];
        }

        return $websiteConfig->toArray();
    }

    public function updateWebsiteConfig(string $cdnDomain, int $id, array $data): array
    {
        try {
            return [self::_RESULT => $this->_websiteConfigObj->updateWebsiteConfig($cdnDomain, $id, $data), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新站点配置项详细信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function deleteWebsiteConfig(string $cdnDomain, int $id): array
    {
        try {
            return [self::_RESULT => $this->_websiteConfigObj->deleteWebsiteConfig($cdnDomain, $id), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键删除站点配置项失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function getWebsiteConfigInfo(string $cdnDomain): array
    {
        $websiteConfigs = [];
        $rows           = $this->getAllWebsiteConfig();
        foreach ($rows as $row) {
            if ($row['status'] == self::_IS_DISABLE_STATUS) {
                continue;
            }
            $configGroup                                       = $row['config_group'];
            $websiteConfigs[$configGroup]['config_group_name'] = WebsiteConfigModel::CONFIG_GROUP_MAP[$configGroup];
            $row['fullConfigValue']                            = $row['config_value'] ? $cdnDomain . '/' . $row['config_value'] : '';
            $websiteConfigs[$configGroup]['website_configs'][] = $row;
        }
        return $websiteConfigs;
    }

    public function batchUpdateWebsiteConfigByCode(string $cdnDomain, array $data): array
    {
        try {
            unset($data['website_code']);
            return [self::_RESULT => $this->_websiteConfigObj->batchUpdateWebsiteConfigByCode($cdnDomain, $data), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过配置标识更新站点配置值信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function batchWebsiteConfigSort(array $data): array
    {
        try {
            return [self::_RESULT => $this->_websiteConfigObj->batchWebsiteConfigSort($data), self::_MESSAGE => self::_SORT_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新站点配置项排序失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }
}
